home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / PCCP024.ARJ / SCRCHK.C < prev    next >
C/C++ Source or Header  |  1992-05-08  |  5KB  |  207 lines

  1. #include<stdio.h>
  2.  
  3. #define PROGSIZ 256
  4.  
  5. struct
  6.     {
  7.     char type;
  8.     unsigned char label;
  9.     }
  10.     miniprog[PROGSIZ];
  11.  
  12. main(argc, argv)
  13.     int argc;
  14.     char **argv;
  15.     {
  16.     FILE *scriptfd;
  17.     char fpname[256], str[81];
  18.     int proglen, i, value[8], flag;
  19.     short int labels[256];
  20.     if(argc!=2)
  21.         {
  22.         printf("USAGE: scrchk <sourcefile>");
  23.         exit(1);
  24.         }
  25.     if(getenv("TERMPATH")==NULL)
  26.         sprintf(fpname, "%s.scr", argv[1]);
  27.     else
  28.         sprintf(fpname, "%s\\%s.scr", getenv("TERMPATH"), argv[1]);
  29.     if((scriptfd=fopen(fpname, "r"))==NULL)
  30.         {
  31.         printf("Error opening script file %s.\n", fpname);
  32.         exit(2);
  33.         }
  34.     fgets(str, 80, scriptfd); /* Flush init params */
  35.     for(i=0;i<256;++i)
  36.         labels[i]=-1;
  37.     /* Parse */
  38.     printf("Parsing...\n");
  39.     for(proglen=0;proglen<PROGSIZ;++proglen)
  40.         {
  41.         if(fgets(str, 80, scriptfd)==NULL)
  42.             {
  43.             flag=1;
  44.             proglen++;
  45.             break;
  46.             }
  47.         for(i=0;i<80;++i)
  48.             if(str[i]=='\n')
  49.                 {
  50.                 str[i]='\0';
  51.                 break;
  52.                 }
  53.             else if(str[i]=='\0')
  54.                 break;
  55.         if(!strlen(str))
  56.             {
  57.             printf("---\n");
  58.             proglen--;
  59.             }
  60.         else
  61.             {
  62.             if((str[0]!=':')&&(str[0]!=';'))
  63.                 printf("%03d %s\n", proglen, str);
  64.             else
  65.                 printf("--- %s\n", str);
  66.             if((str[0]!=';')&&(str[1]!=' '))
  67.                 {
  68.                 printf("*** Missing first delimiting space.\n");
  69.                 exit(11);
  70.                 }
  71.             switch(str[0])
  72.                 {
  73.                 case ':':
  74.                     if(sscanf(str, "%*c %d", &value[0])!=1)
  75.                         {
  76.                         printf("*** Bad scan of label (:)\n");
  77.                         exit(11);
  78.                         }
  79.                     if((value[0]<0)||(value[0]>255))
  80.                         {
  81.                         printf("*** Label out of range\n");
  82.                         exit(11);
  83.                         }
  84.                     if(labels[value[0]]!=-1)
  85.                         {
  86.                         printf("*** Label duplicated\n", value[0], proglen);
  87.                         exit(11);
  88.                         }
  89.                     labels[value[0]]=proglen;
  90.                     proglen--;
  91.                     break;
  92.                 case 'g':
  93.                 case 'G':
  94.                     miniprog[proglen].type='g';
  95.                     if(sscanf(str, "%*c %d", &value[0])!=1)
  96.                         {
  97.                         printf("*** Bad scan\n");
  98.                         exit(11);
  99.                         }
  100.                     miniprog[proglen].label=(unsigned char)value[0];
  101.                     break;
  102.                 case 'r':
  103.                 case 'R':
  104.                     miniprog[proglen].type='r';
  105.                     if(sscanf(str, "%*c %d %d", &value[0], &value[1])!=2)
  106.                         {
  107.                         printf("*** Bad scan\n");
  108.                         exit(11);
  109.                         }
  110.                     miniprog[proglen].label=(unsigned char)value[0];
  111.                     break;
  112.                 case 'p':
  113.                 case 'P':
  114.                     miniprog[proglen].type='p';
  115.                     if(sscanf(str, "%*c %d", &value[0])!=1)
  116.                         {
  117.                         printf("*** Bad scan\n");
  118.                         exit(11);
  119.                         }
  120.                     break;
  121.                 case '>':
  122.                     miniprog[proglen].type='>';
  123.                     if(sscanf(str, "%*c %d", &value[0])!=1)
  124.                         {
  125.                         printf("*** Bad scan\n");
  126.                         exit(11);
  127.                         }
  128.                     miniprog[proglen].label=(unsigned char)value[0];
  129.                     break;
  130.                 case '?':
  131.                     miniprog[proglen].type='?';
  132.                     if(sscanf(str, "%*c %d", &value[0])!=1)
  133.                         {
  134.                         printf("*** Bad scan\n");
  135.                         exit(11);
  136.                         }
  137.                     miniprog[proglen].label=value[0];
  138.                     break;
  139.                 case '<':
  140.                     miniprog[proglen].type='<';
  141.                     break;
  142.                 case '!':
  143.                     miniprog[proglen].type='!';
  144.                     break;
  145.                 case 's':
  146.                     miniprog[proglen].type='s';
  147.                     if(sscanf(str, "%*c %d", &value[0])!=1)
  148.                         {
  149.                         printf("*** Bad scan\n");
  150.                         exit(11);
  151.                         }
  152.                     miniprog[proglen].label=value[0];
  153.                     break;
  154.                 case 'q':
  155.                 case 'Q':
  156.                     miniprog[proglen].type='q';
  157.                     if(sscanf(str, "%*c %d", &value[0])!=1)
  158.                         {
  159.                         printf("*** Bad scan\n");
  160.                         exit(11);
  161.                         }
  162.                     if((value[0]<128)||(value[0]==0))
  163.                         {
  164.                         printf("*** Quit with reserved exit code (!=0&&<128)\n");
  165.                         exit(11);
  166.                         }
  167.                     break;
  168.                 case ';':
  169.                     proglen--;
  170.                     break;
  171.                 default:
  172.                     printf("*** Bad command character\n");
  173.                     exit(10);
  174.                 }
  175.             }
  176.         }
  177.     if(!flag)
  178.         {
  179.         printf("Program too long.\n");
  180.         exit(11);
  181.         }
  182.     /* Check labels */
  183.     printf("\n\nChecking branch label validity...\n");
  184.     for(i=0;i<proglen;i++)
  185.         switch(miniprog[i].type)
  186.             {
  187.             case 'g':
  188.             case '?':
  189.                 if(labels[miniprog[i].label]==-1)
  190.                     printf("Unlisted label %d at statement %d (%c).\n", miniprog[i].label, i, miniprog[i].type);
  191.                 break;
  192.             case 'r':
  193.                 if(labels[miniprog[i].label]==-1)
  194.                     printf("Unlisted label %d at statement %d (%c).\n", miniprog[i].label, i, miniprog[i].type);
  195.                 break;
  196.             case '>':
  197.                 if(labels[miniprog[i].label]==-1)
  198.                     printf("Unlisted label %d at statement %d (%c).\n", miniprog[i].label, i, miniprog[i].type);
  199.                 break;
  200.             case 's':
  201.                 if(labels[miniprog[i].label]==-1)
  202.                     printf("Unlisted label %d at statement %d (%c).\n", miniprog[i].label, i, miniprog[i].type);
  203.                 break;
  204.             }
  205.     printf("\nDone.\n");
  206.     }
  207.